package com.zhao.fannews;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Xml;
import android.widget.ListView;

import com.zhao.fannews.domain.News;
import com.zhao.fannews.units.BitmapTools;
import com.zhao.fannews.units.HttpTools;
import com.zhao.fannews.units.MyAdapter;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.io.PipedReader;
import java.util.LinkedList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private final static int SUCCESS = 1;
    private final static int FAIL = 0;
    private final String hostName = "http://119.29.241.112:8080/buynow/newsData/news.xml";
    private ListView lv_news;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SUCCESS:
                    lv_news.setAdapter(new MyAdapter(MainActivity.this
                            ,R.layout.item_news, (List<News>) msg.obj));
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv_news = (ListView) findViewById(R.id.lv);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    List<News> newsList = new LinkedList<News>();
                    InputStream in = HttpTools.transform(hostName);
                    XmlPullParser parser = Xml.newPullParser();
                    parser.setInput(in,"utf-8");
                    int type = parser.getEventType();
                    News news = null ;
                    while (type != XmlPullParser.END_DOCUMENT) {
                        String name = parser.getName();
                        if (type == parser.START_TAG) {
                            if ("title".equals(name)) {
                                String title = parser.nextText();
                                news.setTitle(title);
                            } else if ("description".equals(name)) {
                                String description = parser.nextText();
                                news.setDescription(description);
                            } else if ("image".equals(name)) {
                                String imageUrl = parser.nextText();
                                Bitmap bitmap = BitmapTools.transform(imageUrl);
                                news.setImage(bitmap);
                            } else if ("item".equals(name)) {
                                news = new News();
                            }
                        }else if(type == parser.END_TAG){
                            if ("item".equals(name)){
                               newsList.add(news);
                            }
                        }
                        type=parser.next();
                    }
                    Message msg = Message.obtain();
                    msg.what=SUCCESS;
                    msg.obj=newsList;
                    handler.sendMessage(msg);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (XmlPullParserException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
